package com.geektime.hello.controller;

import com.geektime.hello.entity.User;
import com.geektime.hello.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @Author DRevelation
 * @Date 2020-08-19 下午 18:08
 * @Desc
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("tx")
public class TxController {

    @Autowired
    private IUserService userService;

    @GetMapping("save")
    public void save() throws Exception {
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setUsername("name-" + i);
            user.setPassword("pwd-" + i);
            user.setStatus("1");
            user.setCreateTime(LocalDateTime.now());
            userService.save(user);
            if (i == 9) {
                throw new Exception("tx exception");
            }
        }
    }

}
